//
//  HomeViewController.swift
//  AppArchitectureSwift
//
//  Created by 胡锦涛 on 2019/2/20.
//  Copyright © 2019 胡锦涛. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController,UserViewDelegate {

    ///Cell重用标识
    let reuseId:String = "reuseId"
    
    ///TableView
    var tableView: UITableView = UITableView()
    
    ///指示器
    var indicator: UIActivityIndicatorView = UIActivityIndicatorView()
    
    ///空白占位
    var emptyView: UIView = UIView()
    
    ///Protocol
    var userPresenter: UserPresenter?
    
    ///数据源
    var dataSource: HKDataSource = HKDataSource()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData()
        setupUI()
       
    }
    
    func loadData() {
        self.userPresenter = UserPresenter()
        self.userPresenter?.delegate = self
        self.userPresenter?.getUsers()
    }
    
    func setupUI() {
        
        self.navigationItem.title = "首页"
        self.view.backgroundColor = kWhiteColor
        self.dataSource = HKDataSource.init(reuseId, configureBlock: { (cell, model, indexPath) in
            (cell as! TableViewCell).setModel((model as? User)!)
        })
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: HK_NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - HK_NAV_HEIGHT), style: .plain)
        self.tableView.tableFooterView = UIView()
        self.tableView.backgroundColor = kWhiteColor
        self.tableView.register(TableViewCell.classForCoder(), forCellReuseIdentifier: reuseId)
        self.view.addSubview(self.tableView)

        self.tableView.dataSource = self.dataSource
        
    }
    
    // MARK: - UserViewDelegate

    func startLoading() {
        self.indicator.isHidden = false
        self.indicator.startAnimating()
    }
    
    func finishLoading() {
        self.indicator.stopAnimating()
        self.indicator.isHidden = true
    }
    
    func setUsers(_ userViewData: [User]) {
        self.dataSource.addDataArray(array: userViewData as NSArray)
        self.tableView.isHidden = false
        self.emptyView.isHidden = true
        self.tableView.reloadData()
    }
    
    func setEmptyUsers() {
        self.emptyView.isHidden = false
        self.tableView.isHidden = true
        self.indicator.isHidden = true
    }

}
